//
//  MDITabBar.swift
//  MDIWeibo
//
//  Created by 迁徙鸟 on 16/4/16.
//  Copyright © 2016年 qianxiniao. All rights reserved.
//

import UIKit

protocol MDITabBarDelegate : NSObjectProtocol{

func clickCompose()

}


class MDITabBar: UITabBar {
    
//    声明一个代理
    weak var MDIDelegate : MDITabBarDelegate?
    
    
    

//    重写init frame方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

//    要重写init frame方法，必须要实现这个方法，告诉使用者，当前的View不支持xib和SB
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK - 撰写按钮的点击事件
    @objc private func clickComposeButton(){
    
        MDIDelegate?.clickCompose()
    
    }
    
//    设置子控件
    func setUpUI(){
    
    addSubview(composeButton)
    }
    
// MARK: - 对子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        // UITabBarButton 系统私有的
        
        // UITabBarButton 宽度
        let width = KWIDTH / 5
        // 使用标示
        var index: CGFloat = 0
        // 遍历tabbar 身上的子控件
        for value in self.subviews{
            // 先判断他的子控件知否是我们的需要的UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                // 设置UITabBarButton x
                value.frame.origin.x = index * width
                // 设置UITabBarButton 宽度
                value.frame.size.width = width
                
                // index ++
                index++

                // 判断index == 2  再++
                if index == 2{
                    index++
                }
            }
        }
        // 设置composeButton center x y
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }

    //    MARK - 懒加载中间的撰写按钮
   private lazy var composeButton: UIButton = {
        let button = UIButton()
    
    button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
    
//  设置图片 
    button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    
    button .sizeToFit()
    
      return button
    }()
    
    
//    给Button设置frame
    
    
    
    
    
    
    
    

}
